<!--  -->
<template>
  <div class="login">
    <div class="header">
      宏烨经纪人
    </div>
    <img src="../../assets/login.png" alt="" class="img" />
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          登录
        </el-button>
        <el-button @click="resetForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login, register } from "@/api";
import { Notify } from "vant";
export default {
  // 组件状态值
  components: {
    Notify,
  },
  data() {
    return {
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  // 计算属性
  computed: {},
  // 组件方法
  methods: {
    //登录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.code === 0) {
              localStorage.setItem("token", res.data.token);
              localStorage.setItem("ruleForm", JSON.stringify(res.data.data));
              this.$router.push("/");
              Notify({ type: "success", message: res.data.msg });
            } else {
              Notify({ type: "danger", message: res.data.msg });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //注册
    resetForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register(this.ruleForm).then((res) => {
            if (res.data.code === 3) {
              Notify({ type: "danger", message: res.data.msg });
            } else {
              Notify({ type: "success", message: res.data.msg });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.header {
  width: 100%;
  line-height: 70px;
  font-size: 20px;
  color: #fff;
  background: #4381e6;
}
.img {
  margin: 0 auto;
}
.el-form {
  width: 320px;
  margin: 10px auto;
  padding-top: 15px;
  border-radius: 10px;
  // box-shadow: 0 0 9px 3px #999;
  .el-input {
    width: 180px;
  }
  .el-button {
    border-radius: 20px;
  }
}
</style>
